<script setup lang="ts">
    import { ref, reactive, onMounted } from 'vue'
    import { genFileId, ElMessage } from 'element-plus'
    import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
    import { useRouter } from "vue-router";
    import SlotsAddProjectOpt from '/src/components/SlotsAddProjectOpt.vue'
    import AddProjectOperateButton from '/src/components/AddProjectOperateButton.vue'
    import JsonEditor from '/src/components/JsonEditor.vue'
    import ProjectName from '/src/components/project/ProjectName.vue'
    import { GetRecord,DueWord } from '@/api/common/dueDiligenceOptRecord.js'


    const router = useRouter();
    const upload = ref<UploadInstance>()
    const loading = ref(false)
    const loadingText = ref('Lodding...')
    const jsonEditorRef = ref(null)

    const data = reactive({
        project:{},
        dueDiligenceOptRecord:{
            disposalResults:{}
        },
        projectCode:'',
        // json编辑器默认展开的层级结构
        jsonexpands: [['债务人尽职调查'],['抵押物尽职调查', '抵押物市场价值（住宅）'],['抵押物尽职调查', '抵押物权属及登记情况'],['担保人尽职调查']]
    })
    const opeateParam = reactive({
        opdateButtons: [
            { name: '生成尽调报告', methond: 'submitUpload' }
        ],
    });
    onMounted(() => {
        data.projectCode = router.currentRoute.value.query.projectCode
        GetRecord(data.projectCode, 2)
            .then((response) => {
                response.data.content.forEach(element => {
                    element.disposalResults = JSON.parse(element.disposalResults)
                });
                data.dueDiligenceOptRecord = response.data.content[0]
            })
            .catch((error) => {});  
    })

    const submitUpload = () => {
        console.log("getJson", jsonEditorRef.value.getJson());
        loadingText.value = '正在进行结果查询，请稍等...'
        // 开启loading
        loading.value = true;
        // setTimeout(() => {
        //     // 关闭loading
        //     loading.value = false;
        //     ElMessage.success('生成尽调报告成功！')
        //     router.push('/bi_frontend/addProject/reportUPdate?projectCode=' + data.projectCode)
        // }, 2000);
        const requestData = {
            projectCode: data.projectCode,
            jsonStr: JSON.stringify(jsonEditorRef.value.getJson())
        };

        DueWord(requestData)
            .then((res) => {
                if(res.code === 200){
                    ElMessage({
                        message: '生成尽调报告成功！',
                        type: 'success',
                    })
                    // 关闭loading
                    loading.value = false;
                    router.push('/bi_frontend/addProject/reportUPdate?projectCode=' + data.projectCode)
                }else{
                    ElMessage({
						message: res.msg||'生成尽调报告异常.',
						type: "eror",
					});
                    // 关闭loading
                    loading.value = false;
                }
            }).catch((error) => { 
                // 关闭loading
                loading.value = false;
            });
    }
</script>

<template>
    <ProjectName :projectCode="data.projectCode"/>
    <SlotsAddProjectOpt
        :loading="loading"
        :loadingText="loadingText"
    >
        <div class="w-100">  
            <!-- 操作区文本框 -->
            <JsonEditor
                ref="jsonEditorRef"
                :json="data.dueDiligenceOptRecord.disposalResults"
                :expand="data.jsonexpands"
            />
            <!-- 操作区按钮组 -->
            <AddProjectOperateButton 
                :opdateButtons="opeateParam.opdateButtons" 
                @submitUpload="submitUpload"
                class="d-flex justify-content-end pt-3 pe-3"
            />
        </div>
    </SlotsAddProjectOpt>
</template>

<style scoped></style>
